<template>
  <div>
    <el-dialog
      v-model="isShowDialog"
      :before-close="handleClose"
      :title="title"
      draggable
      width="80%"
    >
      <el-tabs
        v-model="activeName"
        type="card"
        class="demo-tabs"
        @tab-click="chagenTab"
      >
        <el-tab-pane label="基础信息" name="baseInfo">
          <BaseInfo
            ref="BaseInfoRef"
            :detailData="detailData"
            :isView="isView"
            @setBaseInfo="setBaseInfo"
            @cancel="handleClose"
          ></BaseInfo>
        </el-tab-pane>
        <el-tab-pane label="退货预警设置" name="returnWarn">
          <ReturnWarn
            ref="returnWarnRef"
            :isView="isView"
            :detailData="detailData"
            @clickSave="clickSave"
            @cancel="handleClose"
          ></ReturnWarn>
        </el-tab-pane>
        <!--        <el-tab-pane label="库位设置" name="storageSet">-->
        <!--           <StorageSet ref="storageSetRef" :isView="isView" :detailData="detailData" @setStorageSet="setStorageSet" @cancel="handleClose"></StorageSet>-->
        <!--        </el-tab-pane>-->
        <!-- <el-tab-pane label="关联店铺SKU设置" name="skuSet">
          <SkuSet
            ref="skuSetRef"
            :isView="isView"
            :detailData="detailData"
            @clickSave="clickSave"
            @cancel="handleClose"
          ></SkuSet>
        </el-tab-pane> -->
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script setup>
import { getCurrentInstance } from "vue";
import BaseInfo from "@/views/commodity/commodityManagement/component/baseInfo.vue";
import ReturnWarn from "@/views/commodity/commodityManagement/component/returnWarn.vue";
import StorageSet from "@/views/commodity/commodityManagement/component/storageSet.vue";
import SkuSet from "@/views/commodity/commodityManagement/component/skuSet.vue";
import { skuSave } from "@/api/scApi";

let activeName = ref("baseInfo");
const { proxy } = getCurrentInstance();
const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false,
  },
  title: { type: String },
  detailData: { type: Object },
  isView: { type: Boolean },
});
const emits = defineEmits(["cancel"]);
function handleClose() {
  emits("cancel");
}
let saveParams = ref({});
//设置基本信息数据
function setBaseInfo(obj) {
  activeName.value = "returnWarn";
  saveParams.value.header = { ...saveParams.value.header, ...obj };
}

//设置退货预警数据
// function setReturnWarn(obj) {
//   activeName.value = "skuSet";
//   saveParams.value.header = { ...saveParams.value.header, ...obj };
// }

//设置库位设置数据
// function setStorageSet(obj) {
//   activeName.value = "skuSet";
//   saveParams.value.locationDetails = obj;
// }

//设置关联店铺SKU设置
//数据全部填写完毕后 提交数据
function clickSave(obj) {
  // saveParams.value.skuDetails = obj;
  saveParams.value.header = { ...saveParams.value.header, ...obj };
  //检查数据是否填写完毕
  let header = saveParams.value.header; //基本信息 商品库存预警
  let locationDetails = saveParams.value.locationDetails; //库位
  let skuDetails = saveParams.value.skuDetails; //关联SKU
  if (!header) {
    proxy.$modal.msgWarning("请检查基础信息和退货预警设置是否填写完毕！");
    return;
  }
  if (header) {
    if (!header.skuName || !header.classifyId || !header.attrName) {
      proxy.$modal.msgWarning("请检查基础信息是否填写完毕！");
      return;
    }

    if (!header.earlyWarnStatus && header.earlyWarnStatus != 0) {
      proxy.$modal.msgWarning("请检查退货预警设置是否填写完毕！");
      return;
    }
  }

  // if(locationDetails.length==0){
  //   proxy.$modal.msgWarning("请设置库位！")
  //   return
  // }
  // if(skuDetails.length==0){
  //   proxy.$modal.msgWarning("请关联SKU！")
  //   return
  // }

  skuSave(saveParams.value).then((res) => {
    if (res.code === 200) {
      proxy.$modal.msgSuccess("保存成功！");
      emits("cancel", true);
    }
  });
}
//切换tab页签 保存页面数据
function chagenTab() {
  let baseData = proxy.$refs.BaseInfoRef.getData();
  let returnWarnData = proxy.$refs.returnWarnRef.getData();
  saveParams.value.header = {
    ...saveParams.value.header,
    ...baseData,
    ...returnWarnData,
  };
  // let storageSetData=proxy.$refs.storageSetRef.getData()
  // saveParams.value.locationDetails =storageSetData
  let skuSetData = proxy.$refs.skuSetRef.getData();
  saveParams.value.skuDetails = skuSetData;
}
</script>

<style scoped lang="scss"></style>
